//
//  BLSocketData.swift
//  danmuDemo
//
//  Created by BigL055 on 16/6/14.
//  Copyright © 2016年 bigL.MoreDemo.com. All rights reserved.
//

import UIKit

class BLSocketData: NSObject {
    
    class func douyuData(data : NSData, isAuthData : Bool) {
        
        var contents : [String] = []
        var subData : NSData = data.copy() as! NSData
        var _loction : NSInteger = 0
        var _length : NSInteger = 0
        
        while _loction < data.length {
            _loction += 12
            //获取数据长度
            if subData.length < 12 {
                return
            }
            subData.getBytes(&_length, range: NSMakeRange(0, 4))
            _length -= 8
            //截取相对应的数据
            guard _length < subData.length else{
                return
            }
            let contentData : NSData = subData.subdataWithRange(NSMakeRange(12, _length))
            
            guard let content : String = String(data: contentData, encoding: NSUTF8StringEncoding) else{
                return
            }
            
            //截取余下的数据
            subData = data.subdataWithRange(NSMakeRange(_length + _loction, data.length - _length - _loction))
            contents.append(content)
            _loction += _length
        }
        
        if isAuthData {
            readAuthMsg(contents)
        }else{
            readDanmuMsg(contents)
        }
        
    }
    
    class func readDanmuMsg( array : [String] ) {
        
        let danmuSocket = BLDanmuSocket.sharedInstance
        if array.count == 1 {
            let string = array.first!
            if string =~ DanmuTypeRegex.isLoginres.rawValue {
                //加入弹幕组
                //"-9999" fix :  第二次弹幕无法获取:
                danmuSocket.groupID = "-9999"
                let jionGroup = "type@=joingroup/rid@=\(danmuSocket.room)/gid@=\(danmuSocket.groupID)/"
                let jGroupData : NSData = danmuSocket.packToData(jionGroup)
                danmuSocket.socket?.writeData(jGroupData, withTimeout: 30, tag: 1)
                //开始发送心跳包
                danmuSocket.startKLTimer()
            }else{
                NSNotificationCenter.defaultCenter().postNotificationName(kReceiveMessageNotification, object: string)
            }
        }else{
            for msg : String in array {
                NSNotificationCenter.defaultCenter().postNotificationName(kReceiveMessageNotification, object: msg)
            }
        }
        
    }
    
    
    class func readAuthMsg(array : [String]) {
        
        let authSocket = BLAuthSocket.sharedInstance
        //遍历数组，提取ID
        for msg : String in array {
            
            if msg =~ DanmuTypeRegex.isLoginres.rawValue {
                /*
                 type@=loginres/
                 userid@=1300813515/
                 roomgroup@=0/
                 pg@=0/
                 sessionid@=1134632174/
                 username@=visitor283515/
                 nickname@=visitor283515/
                 live_stat@=1/
                 is_illegal@=0/
                 ill_ct@=/
                 ill_ts@=0/
                 now@=1466164749/
                 ps@=0/es@=0/it@=0/its@=0/npv@=0/
                 best_dlev@=0/cur_lev@=0/nrc@=0/
                 */
                
                
                let vistorID = bl_regexWithPattern(DanmuParameterRegex.UserName.rawValue, inputStr: msg as String).first ?? ""
                authSocket.vistorID = vistorID
            }
            if msg =~ DanmuTypeRegex.isSetMsgGroup.rawValue {
                
                /*
                 type@=setmsggroup/
                 rid@=266055/
                 gid@=26/
                 */
                
                let groupID = bl_regexWithPattern(DanmuParameterRegex.GroupID.rawValue, inputStr: msg as String).first ?? ""
                
                authSocket.groupID = groupID
            }
            
        }
        //将2个ID传出去
        if authSocket.vistorID.characters.count != 0 && authSocket.groupID.characters.count != 0 {
            print("---获得游客ID以及弹幕组---")
            authSocket.InfoBlock!(vistorID: authSocket.vistorID,groupID: authSocket.groupID)
            
            authSocket.cutOffSocket()
        }
    }
}
